package com.gitee.grower.core.kether

import ac.github.oa.internal.core.item.ItemInstance
import org.bukkit.entity.Player
import taboolib.common.platform.function.info
import taboolib.module.kether.*
import java.util.concurrent.CompletableFuture

class ActionItem(val field: ItemField) : ScriptAction<Any>() {
    override fun run(frame: ScriptFrame): CompletableFuture<Any> {
        val player = frame.player().castSafely<Player>() ?: error("No sender selected")
        val itemStack = frame.itemStack()
        return CompletableFuture.completedFuture(field.get(player, itemStack))
    }

    class ActionData(val key: String) : ScriptAction<String>() {
        override fun run(frame: ScriptFrame): CompletableFuture<String> {
            val itemInstance = frame.itemInstance()
            return CompletableFuture.completedFuture(itemInstance.getSession()[key])
        }

    }

    companion object {

        @KetherParser(["item"], shared = true, namespace = "item-grower")
        fun parser() = scriptParser {
            val token = it.nextToken()
            try {
                ActionItem(ItemField.valueOf(token.uppercase()))
            }catch (e: Exception) {
                e.printStackTrace()
                ActionData(token)
            }
        }

    }

}